"""
散客比赛模型
"""
from sqlalchemy import Column, String, Float, Integer, DateTime, JSON
from sqlalchemy.sql import func
from app.core.database import Base


class Match(Base):
    """散客比赛表"""
    __tablename__ = "matches"

    home_team_id = Column(String(64), nullable=False)   # 主队
    away_team_id = Column(String(64), nullable=False) 
    
    id = Column(String(36), primary_key=True, index=True)
    venue_id = Column(String(36), nullable=False, index=True)
    venue_name = Column(String(200), nullable=False)
    
    # 位置信息
    latitude = Column(Float)
    longitude = Column(Float)
    address = Column(String(500))
    
    # 时间信息
    date = Column(String(10), nullable=False, index=True)  # YYYY-MM-DD
    start_time = Column(String(5), nullable=False)  # HH:mm
    end_time = Column(String(5), nullable=False)  # HH:mm
    deadline = Column(DateTime(timezone=True))  # 报名截止时间
    
    # 场地类型和价格
    type = Column(String(10), nullable=False)  # "11", "7", "5"
    price_per_person = Column(Float, nullable=False)
    
    # 人数信息
    current_players = Column(Integer, default=0)
    max_players = Column(Integer, nullable=False)
    min_players = Column(Integer, default=1)
    
    # 发布者信息
    publisher_id = Column(String(36), nullable=False, index=True)
    publisher_name = Column(String(100), nullable=False)
    
    # 位置需求
    positions = Column(JSON)  # {forward: {current: 0, total: 2}}
    
    # 备注说明
    note = Column(String(500))
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
    
    def to_dict(self):
        """转换为字典"""
        return {
            "id": self.id,
            "homeTeamId": self.home_team_id,
            "awayTeamId": self.away_team_id,
            "venueId": self.venue_id,
            "venueName": self.venue_name,
            "location": {
                "latitude": self.latitude,
                "longitude": self.longitude,
                "address": self.address
            },
            "date": self.date,
            "startTime": self.start_time,
            "endTime": self.end_time,
            "type": self.type,
            "pricePerPerson": self.price_per_person,
            "currentPlayers": self.current_players,
            "maxPlayers": self.max_players,
            "minPlayers": self.min_players,
            "deadline": self.deadline.isoformat() if self.deadline else None,
            "publisherId": self.publisher_id,
            "publisherName": self.publisher_name,
            "positions": self.positions or {},
            "note": self.note,
            "players": []  # 需要从 MatchPlayer 表查询
        }


class MatchPlayer(Base):
    """散客比赛球员表"""
    __tablename__ = "match_players"
    
    id = Column(String(36), primary_key=True, index=True)
    match_id = Column(String(36), nullable=False, index=True)
    user_id = Column(String(36), nullable=False, index=True)
    user_name = Column(String(100), nullable=False)
    avatar = Column(String(500))
    position = Column(String(20))  # forward, midfielder, defender, goalkeeper
    rating = Column(Float, default=0.0)
    join_time = Column(DateTime(timezone=True), server_default=func.now())
    
    def to_dict(self):
        """转换为字典"""
        return {
            "userId": self.user_id,
            "userName": self.user_name,
            "avatar": self.avatar,
            "position": self.position,
            "rating": self.rating,
            "joinTime": self.join_time.isoformat() if self.join_time else None
        }

